package com.bclz.practice

import scala.annotation.tailrec

object RecursionPractice {


  def getFibonaccis(n:Int): Int ={
      require(n>0,s"${n} < 0,参数必须大于0!!!")
      @tailrec
      def tailFunction(last:Int,current:Int,count:Int): Int ={
                if(count==1) return current
                else return  tailFunction(current,current+last,count-1)
      }
    tailFunction(1,0,n)
  }

  /**
    * 检测数组是否按照给定的规则排序
    * @param arr      数组
    * @param orderd   规则函数
    * @tparam A
    * @return
    */
  def isSort[A](arr:Array[A],orderd:(A,A)=>Boolean): Boolean ={
        @tailrec
        def judgeOrderd(indx:Int):Boolean={
          if(indx==arr.length-1) return true
          if(orderd(arr.apply(indx),arr.apply(indx+1))) judgeOrderd(indx+1)
          else return false
        }

        judgeOrderd(0)
  }

  def main(args: Array[String]): Unit = {

    //0 1 1 2 3 5 8
    println("第6个斐波那契数: "+getFibonaccis(7))

    println(isSort[Int](Array(1,2,3,6,8),(n1,n2)=>n1<n2))

  }

}
